package nju.software.xkxt.web.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nju.software.xkxt.biz.vo.CourseVO;
import nju.software.xkxt.service.CourseService;
import nju.software.xkxt.service.model.CourseModel;
import nju.software.xkxt.service.model.UserContextModel;
import nju.software.xkxt.util.DateUtil;
import nju.software.xkxt.web.json.ResponseBuilder;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class CourseController {

	CourseService courseService;

	public void setCourseService(CourseService courseService) {
		this.courseService = courseService;
	}

	/**
	 * 显示老师已经发布的课程信息
	 * 
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/teacherhome.do", method = RequestMethod.GET)
	public String showCourse(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		List<CourseVO> courseVOs = new ArrayList<CourseVO>();
		UserContextModel userContextModel = (UserContextModel) request
				.getSession().getAttribute("userContextModel");
		String yhm = userContextModel.getName();
		int teacherId = userContextModel.getUserId();
		String tid = String.valueOf(teacherId);
		List<CourseModel> courseModels = courseService
				.getCourseByTId(teacherId);
		for (CourseModel courseModel : courseModels) {
			CourseVO courseVO = new CourseVO(courseModel);
			courseVOs.add(courseVO);
		}
		map.addAttribute("courseList", courseVOs);
		map.addAttribute("yhm", yhm);
		map.addAttribute("tid", tid);
		return "teacherhome";
	}

	/**
	 * 显示增加课程的页面
	 * 
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/showAddCourse.do", method = RequestMethod.GET)
	public void showAddCourse(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		UserContextModel userContextModel = (UserContextModel) request
				.getSession().getAttribute("userContextModel");
		String tid = userContextModel.getUserId().toString();
		ResponseBuilder responseBuilder = new ResponseBuilder();
		try {
			responseBuilder.writeJsonResponse(response, tid);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 增加课程并保存课程页面
	 * 
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/addcourse.do", method = RequestMethod.POST)
	public String addCourse(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		String c_name = request.getParameter("name");
		String c_type = request.getParameter("type");
		String time = request.getParameter("time");
		String tid = request.getParameter("tid");
		CourseVO courseVO = new CourseVO();
		courseVO.setCName(c_name);
		courseVO.setCType(c_type);
		courseVO.setTime(time);
		courseVO.settId(tid);
		CourseModel cModel = new CourseModel();
		cModel.setTId(Integer.parseInt(courseVO.gettId()));
		cModel.setCType(Integer.parseInt(courseVO.getCType()));
		cModel.setCName(courseVO.getCName());
		cModel.setTime(DateUtil.parse(courseVO.getTime(), DateUtil.webFormat));
		courseService.addCourse(cModel);
		return "redirect:/teacherhome.do";
	}

	/**
	 * 显示修改课程页面
	 * 
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/showpop.do", method = RequestMethod.POST)
	public void showUpdateCourse(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		String cId = request.getParameter("c_id");

		CourseModel courseModel = courseService.getCourseById(Integer
				.parseInt(cId));
		CourseVO courseVO = new CourseVO(courseModel);

		ResponseBuilder responseBuilder = new ResponseBuilder();
		try {
			responseBuilder.writeJsonResponse(response, courseVO);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 修改并保存课程页面
	 * 
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/updateCourse.do", method = RequestMethod.POST)
	public void updateCourse(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) {
		// 获得页面上传来的参数
		String t_id = request.getParameter("tid");
		String c_id = request.getParameter("bh");
		String c_name = request.getParameter("name");
		String c_type = request.getParameter("type");
		String time = request.getParameter("time");

		CourseVO courseVO = new CourseVO();
		courseVO.setCId(c_id);
		courseVO.setCName(c_name);
		courseVO.setCType(c_type);
		courseVO.setTime(time);
		courseVO.settId(t_id);
		CourseModel cModel = new CourseModel();
		cModel.setTId(Integer.parseInt(courseVO.gettId()));
		cModel.setCType(Integer.parseInt(courseVO.getCType()));
		cModel.setCId(Integer.parseInt(courseVO.getCId()));
		cModel.setCName(courseVO.getCName());
		cModel.setTime(DateUtil.parse(courseVO.getTime(), DateUtil.webFormat));
		boolean isSuccess = courseService.updateCourse(cModel);

		ResponseBuilder responseBuilder = new ResponseBuilder();
		try {
			responseBuilder.writeJsonResponse(response, isSuccess);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
